<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";

 require "../../misc/class_tag.php";
 require "../../misc/tool_color.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../pear_graph/tool_pear_graph.php";

 require "../../maths/tool_maths.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";

 require "../class_sql_document.php";

$dir="../../xml/europa_complete_repair/";
$lg_candidates=array("cs","da","de","el","en","es","et","fi","fr","hu","it","lt","lv","nl","pl","pt","sl","sw");
$array_ids_europa=tool_corpus::get_unified_europa($dir,$lg_candidates);
$motif_lg="fr";

$exp=new sql_experience("align_word00");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data=$array_obj_sql['sql_graphie']->reader_all2exec();
foreach($data as $ligne){
print_r($ligne);
}
die();
$factory_mot=new factory_mot();
foreach($data as $ligne){
  $factory_mot->get_all_mots($ligne['contenu_text']);
}

$dictionnaire=$factory_mot->__get_dictionnaire();
foreach($dictionnaire as $graphie=>$array_doc){
  $id_graphie=$array_obj_sql['sql_graphie']->writer_values(array('graphie'=>$graphie));
  $array_request=array();

  foreach($array_doc as $id_doc=>$array_bloc){
    foreach($array_bloc as $id_bloc=>$array_segment){
      foreach($array_segment as $id_segment){
        $mot=$factory_mot->__get_word($id_segment);
        $array_request[]=array(
          'id_bloc'=>$id_bloc+1,
          'id_graphie'=>$id_graphie,
          'offset'=>$mot->__get_offset(),
          'len'=>$mot->__get_len()
        ); 
      }
    }
  }

  $array_obj_sql['sql_rstr']->writer_array_values($array_request);
  unset($array_request);
}

die();


$data=$array_obj_sql["sql_document"]->reader_all2exec();
$array_data=array();
foreach($data as $ligne){
  $document=new document_xml($ligne['path_doc'],$ligne['contenu']);
  $structure=$document->__get_structure();
  $signature=$structure->__get_signature();
  tool_tree::cleanstructure($signature);
  $signature->__recalc_idn();

  $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);
  $array_bloc=array();
  foreach($test as $id_bloc=>$bloc_xhtml){
    $xhtml=tool_tree::tree_xhtml2xhtml($bloc_xhtml->__get_value());
    $texte=tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
    if($cpt>1) die();
    $array_data[]=array(
      'id_document'=>$ligne['id_document'],
      'contenu_text'=>$texte,
      'contenu_xhtml'=>$xhtml
    );
  }
  $array_obj_sql['sql_bloc']->writer_array_values($array_data);
  unset($array_data);
  $array_data=array();
}

die();


foreach($lg_candidates as $lg){
  foreach($array_ids_europa as $id_europa){  
    $corpus=tool_corpus::get_corpus_regexp($dir,$id_europa."($lg)");
    $array_doc=$corpus->__get_array_doc();
    foreach($array_doc as $document){
      $long_id_doc=$document->__get_id();
      $explode=explode('/',$long_id_doc);
      $file=$explode[4];
      $contenu=$document->__get_container();
      $array_data[]=array(
        'path_doc'=>$file,
        'lg'=>$lg,
        'contenu'=>$contenu
      );
    }
    $array_obj_sql['sql_document']->writer_array_values($array_data);
    unset($array_data);
    $array_data=array();
  } 
}

die();


//$array_ids_europa=tool_corpus::europa_get_all_ids($dir);
$array_corpus_groupe=array();
$start=microtime(true);
$cpt=0;
$taille=0;
$array_bloc_str=array();
$array_doc_bloc=array();
$array_bloc_doc=array();
$factory_mot=new factory_mot();
foreach($array_ids_europa as $id_europa){
  $corpus=tool_corpus::get_corpus_regexp($dir,$id_europa."($motif_lg)");
  $array_doc=$corpus->__get_array_doc();
  foreach($array_doc as $document){
    $long_id_doc=$document->__get_id();
    $explode=explode('/',$long_id_doc);
    $file=$explode[3];
    $id_doc=mb_strcut($document->__get_id(),-6,2);
    $structure=$document->__get_structure();
    $signature=$structure->__get_signature();
    tool_tree::cleanstructure($signature);
    $signature->__recalc_idn();

    $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);
    $array_bloc=array();
    foreach($test as $id_bloc=>$bloc_xhtml){
      $array_bloc[]=$cpt;
      $array_bloc_doc[$id_doc][$cpt]=$long_id_doc;
      $array_bloc_str[] = tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
      $factory_mot->get_all_mots($array_bloc_str[count($array_bloc_str)-1],$id_doc);
      $cpt++;
    }
    $array_doc_bloc[$id_doc][]=$array_bloc;
  }
}

print_r($array_doc_bloc);

die();

//print_r($array_doc_bloc);
//print_r($array_bloc_doc);
//die();
//$all_seg=$factory_mot->__get_array_segment();
$end=microtime(true)-$start;
//echo "\n total :: $end $cpt\n";

//$factory_mot->init_etiquetage();
$factory_mot->init_etiquetage_local($array_doc_bloc);
//$start=microtime(true);
//$factory_mot->init_chunk();
//$end=microtime(true)-$start;
//echo "\n total :: $end";
//die();


//$repartition_chunk=$factory_mot->__get_repartiton_chunk();
$repartition_segment=$factory_mot->__get_repartition_segment();
$res_all=$zipf=$res_type=array();
$res_type["p"]=$res_type["v"]=$res_type["i"]=0;
foreach($repartition_segment as $id_doc=>$array_bloc){
  foreach($array_bloc as $id_bloc=>$array_id_segment){
    foreach($array_id_segment as $id_segment){
      $mot=$factory_mot->__get_word($id_segment);
      $len=$mot->__get_len();
      $str=mb_strtolower($mot->__tostring());
      $type=$mot->__get_attribute("type");
      $id_doc=mb_strcut($id_doc,-6,2);
      ++$zipf[$id_doc][$str];
      ++$zipf_type[$id_doc][$type][$str];
    }
  }
}

foreach($zipf_type as $id_doc=>$array_type){
  foreach($array_type as $type=>$array_str){
    ob_start();
    arsort($array_str);
    foreach($array_str as $str=>$nb_occ){
      echo "$nb_occ $str\n";
    }
    $detail_type=ob_get_contents();
    Tool_files::file_write("./etiquetage/type_".$type."_".$id_doc.".txt",$detail_type);
    ob_clean();
  }
}

die();
function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot04();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

$tool_graph=new Tool_pear_graph();
$list_ids_doc="";
foreach($zipf as $id_doc => $res_zipf){
  $list_ids_doc.="[$id_doc]";
  $zipf=tool_maths::zipf($res_zipf);
  fill_graph_aux($tool_graph,array_keys($zipf),array_values($zipf));
  $tool_graph->__add_plot_title("zipf : $id_doc");
}

$tool_graph->__add_title("zipf");
$graph=$tool_graph->plot04();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf".$list_ids_doc.".svg",$graph_svg);
ob_clean();
die();

?>
